package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.ConfigSystem;

import admin.models.MetaSeoModel;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import entities.MetaSeo;
import entities.SiteConfig;

public class MessageServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		SiteConfig config = ConfigSystem.getConfigs();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String type = URLDecoder.decode(path.toLowerCase().replaceAll("/msg/", ""),"UTF-8");
		
		String title = "Message Alert";
		String msg = "<a href=\"/home\">Go to home</a>";
		
		if (type != null && type.equalsIgnoreCase("not-permission")) {
			msg = "Sorry, you don't enought permission access this page! Please go back or " + msg;
		}
		
		if (type != null && type.equalsIgnoreCase("page-not-found")) {
			title = "Page Not Found";
			msg = "Sorry, this page is not exists! Please go back or " + msg;
		}
		
		if (type != null && type.equalsIgnoreCase("blacklist")) {
			title = "Page Not Found";
			msg = "Sorry, this website is in my blacklist. Please go back or " + msg;
		}
		
		
		if (type != null && type.equalsIgnoreCase("require-login")) {
			msg = "Sorry, you need login for access this page! You can <a href=\"/login\">login here</a>";
		}
		
		if (type != null && type.equalsIgnoreCase("logout-success")) {
			msg = "Thank you! You have been logout success. You can <a href=\"/login\">login here</a>";
		}
		
		
		MetaSeoModel metaseoModel = MetaSeoModel.getInstance();
		MetaSeo metaseo = metaseoModel.get("message");
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("title", title);
		request.setAttribute("msg", msg);
		request.setAttribute("metaseo", metaseo);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/message.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
